package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.dto.ElderDeviceDto;
import com.zzyl.nursing.vo.ElderDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人Service业务层处理
 *
 * @author JBY
 * @date 2025-08-11
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     *
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id) {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     *
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     *
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     *
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     *
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return elderMapper.deleteById(id);
    }

    @Override
    public TableDataInfo<ElderDeviceVo> pageQuery(ElderDeviceDto elderDeviceDto) {
        String name = elderDeviceDto.getName();
        String idCardNo = elderDeviceDto.getIdCardNo();
        Integer status = elderDeviceDto.getStatus();

        Page<Elder> page = new Page<>(elderDeviceDto.getPageNum(), elderDeviceDto.getPageSize());
        this.lambdaQuery().eq(Objects.nonNull(status),Elder::getStatus, status)
                .like(StringUtils.isNotEmpty(name),Elder::getName, name)
                .like(StringUtils.isNotEmpty(idCardNo),Elder::getIdCardNo, idCardNo)
                .page(page);

        List<ElderDeviceVo> list = BeanUtil.copyToList(page.getRecords(), ElderDeviceVo.class);

        TableDataInfo<ElderDeviceVo> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
